package com.elitel.frame.main.config.datasourceconfig;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Configuration
@MapperScan(basePackages = {"com.elitel.frame.main.dao", "com.elitel.frame.main.dao.ext",
        "com.elitel.quartz.mapper",
        "com.elitel.frame.web.*.dao",
        "com.elitel.frame.main.dao.system"}, sqlSessionFactoryRef = "baseSqlSessionFactory")

public class MasterDBConfig {
    @Bean(name = "defaultDataSource")
    @ConfigurationProperties(prefix = "spring.db")
    @Primary
    public DataSource ssoDataSource() {
        //指定使用DruidDataSource
        return DataSourceBuilder.create().type(com.alibaba.druid.pool.DruidDataSource.class).build();
    }

    @Bean(name = "baseSqlSessionFactory")
    @Primary
    public SqlSessionFactory baseSqlSessionFactory(@Qualifier("defaultDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        ResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();
        bean.setDataSource(dataSource);
        List<Resource> resourceList = new ArrayList<Resource>();
        Resource[] resources = resolver.getResources("classpath:com/elitel/frame/main/dao/mapping/**/*.xml");
        Resource[] resolverResources = resolver.getResources("classpath*:mapper/**/*.xml");
        resourceList.addAll(Arrays.asList(resources));
        resourceList.addAll(Arrays.asList(resolverResources));

        bean.setMapperLocations(resourceList.toArray(new Resource[resourceList.size()]));

        bean.setConfigLocation(new PathMatchingResourcePatternResolver().getResource("classpath:mybatis/mybatis-config.xml"));
        return bean.getObject();
    }

    @Primary
    @Bean(name = "baseTransactionManager")
    public DataSourceTransactionManager testTransactionManager(@Qualifier("defaultDataSource") DataSource dataSource) {
        return new DataSourceTransactionManager(dataSource);
    }

    @Primary
    @Bean(name = "baseSqlSessionTemplate")
    public SqlSessionTemplate testSqlSessionTemplate(@Qualifier("baseSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }
}
